<?php

namespace Home\Controller;

class GoodsController extends BaseController {

    public function _initialize() {
        $userToken = '';
        if (I("user_token")) {
            $userToken = I("user_token");
        }
        //查询库
        $where['user_token'] = $userToken;
        $userTokenInfo = M('Users')->where($where)->find();
        if ($userTokenInfo) {
            $this->user_id = $userTokenInfo['user_id'];
        }
    }

    public function info() {
        $goods_id = I('goods_id', 0, 'intval');

        if ($goods_id == 0) {
            $this->error('错误的商品');
        }

        $where['is_del'] = 0;
        $where['goods_id'] = $goods_id;
        $goods_info = M('Goods')
                        ->field('goods_id,goods_name,goods_pic,price,explain,qcode,goods_desc,collect_num,status')
                        ->where($where)->find();

        if ($goods_info) {
            $goods_pic = explode(',', $goods_info['goods_pic']);
            if ($goods_pic) {
                foreach ($goods_pic as &$val) {
                    $val = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val;
                }
                unset($val);
            } else {
                $goods_pic = [];
            }


            $goods_info['goods_pic'] = $goods_pic;

            $goods_info['explain'] = str_replace('<img src="', '<img src="' . 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__, $goods_info['explain']);

//            $qcode = explode(',', $goods_info['qcode']);
//            if ($qcode) {
//                foreach ($qcode as &$val) {
//                    $val = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val;
//                }
//                unset($val);
//            } else {
//                $qcode = [];
//            }
//
//            $goods_info['qcode'] = $qcode;


            $goods_attr = M('GoodsAttr')->field('attr_name,attr_value')->where(['goods_id' => $goods_info['goods_id']])->order('sort')->select();

            if (empty($goods_attr)) {
                $goods_attr = [];
            }

            $goods_info['goods_attr'] = $goods_attr;

            $is_collect = 0;
            if ($this->user_id != 0) {

                $is_collect = M('Collect')->where(['goods_id' => $goods_id, 'user_id' => $this->user_id])->find();
                if ($is_collect) {
                    $is_collect = 1;
                }
            }

            $goods_info['is_collect'] = $is_collect;
        }

        $this->success('获取成功', $goods_info);
    }

    public function getList() {
        $center_id = I('center_id', 0, 'intval');
        $cate_id = I('cate_id');
        $sort = I('sort', -1, 'intval');
        $sort_type = I('sort_type', 0, 'intval');
        $keywords = I('keywords');

        $p = I('p', 0, 'intval');

        if ($p) {
            $page = $p * 10;
        }

        $where = ' is_del = 0 and status = 0';

//        $where['is_del'] = 0;
//        $where['status'] = 0;

        if ($cate_id) {
            $cate_ids = explode(',', $cate_id);
//            $cate_ids = implode(',%,', $cate_ids);

            $where .= ' and (';
            foreach ($cate_ids as $cid) {
                $where .= " cate_id like '%," . $cid . ",%' or";
            }
//            $where['cate_id'] = array('like', '%,' . $cate_ids . ',%');
            $where = rtrim($where, 'or');
            $where .= ")";

//            $map['title'] = array('like', '%thinkphp%');
        }
//
//        var_dump($where);
//        die;

        if ($center_id != 0) {
            $where .= " and center_id like '%," . $center_id . ",%' ";
//            $where['center_id'] = array('like', '%' . $center_id . '%');
        }

        $order = 'create_time desc';
        if ($sort != -1) {
            if ($sort == 1) {
                $order = 'create_time';
            }

            if ($sort == 2) {
                $order = 'collect_num';
            }

            if ($sort == 3) {
                $order = 'price';
            }

            if ($sort_type == 0) {
                $order .= ' desc';
            }
        }

        if ($keywords) {
            $is_search = M('SearchKeywords')->where(['keywords' => $keywords])->find();

            if ($is_search) {
                M('SearchKeywords')->where(['key_id' => $is_search['key_id']])->setInc('search_num');
            } else {
                M('SearchKeywords')->add(['keywords' => $keywords]);
            }

            $where .= " and goods_name like '%" . $keywords . "%' ";
//            $where['goods_name'] = array('like', '%' . $keywords . '%');
        }
        
        $goods_list = M('Goods')
                ->field('goods_id,goods_name,head_img,price')
                ->where($where)
                ->order($order)
                ->limit($page, 10)
                ->select();


        if ($goods_list) {
            foreach ($goods_list as &$val) {
                $val['head_img'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['head_img'];
            }
            unset($val);
        } else {
            $goods_list = [];
        }

        $this->success('获取成功', $goods_list);
    }

}
